//
//  AppDelegate.m
//  InformationPovertyAlleviation
//
//  Created by 梁素珍 on 2018/10/16.
//  Copyright © 2018年 梁素珍. All rights reserved.
//

#import "AppDelegate.h"
#import "BaiduMobStat.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


//此方法会在设备横竖屏变化的时候调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.rotateDirection == 1){
        return UIInterfaceOrientationMaskLandscapeRight; // 支持右屏旋转
    }
    return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow *window in windows) {
        if(window.rootViewController == nil){
            UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
            window.rootViewController = vc;
        }
    }
    // 文件路径
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *result = NullStringjudgment([defaults objectForKey:@"token"]); //dict[@"token"];
    //    NSLog(@"%@>>>token",result);
    if(result.length == 0){
        LSZPasswordLoginVC * vc = [[LSZPasswordLoginVC alloc]init];
        [self.window setRootViewController:vc];
    }else{
        [InterfaceJumpEncapsulation jumpToNewTabBar:0];
    }
    [[UINavigationBar appearance]setBarTintColor:[UIColor redColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    //    在appDelegate中通知app支持后台播放
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:YES error:nil];
    [LSZRequestData detectionVersionUpdate:^(BOOL isOK, NSString * str, NSDictionary * dic) {
        if(isOK == YES){
            NSInteger versionNumber = [dic[@"versionNumber"] integerValue];
            NSLog(@"%d>>>>",currentVersionNumber);
            if (versionNumber > currentVersionNumber) {
                //需要版本更新
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:dic[@"summary"]   preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
                    //id后的需要换成这个APP的
                    NSString * url = [[NSString alloc]initWithFormat:@"http://itunes.apple.com/app/id%@",@"1437713973"];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
                }];
                //先设置不强更
                if ([dic[@"force"] integerValue] == 1) {
                }else{
                    //不强更
                    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
                        NSLog(@"取消");
                    }];
                    [alert addAction:cancelAction];
                }
                [alert addAction:okAction];
                [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
            }
        }
    }];
    [[BaiduMobStat defaultStat] startWithAppId:@"d111088257"];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
